package com.example.stugradeapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.facebook.stetho.Stetho
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val myDB= MyDatabaseHelper(this)
    var book_id = ArrayList<Int>()
    var book_title = ArrayList<String>()
    var book_author = ArrayList<String>()
    var book_pages = ArrayList<Int>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Stetho.initializeWithDefaults(this)
        add_button.setOnClickListener(){
            val intent  = Intent(this,AddActivity::class.java)
            startActivity(intent)
        }
        storeDatainArrays()
        val customAdapter = CustomeAdapter(this,book_id,book_title,book_author,book_pages)
        recycleView.adapter=customAdapter
        recycleView.layoutManager=LinearLayoutManager(this)
    }

    fun storeDatainArrays(){
        val cursor = myDB.readData()
        if(cursor.columnCount==0){
            Toast.makeText(this,"no data",Toast.LENGTH_SHORT).show()
        }else{
            while(cursor.moveToNext()){
                book_id.add(cursor.getString(0).toInt())
                book_title.add(cursor.getString(1))
                book_author.add(cursor.getString(2))
                book_pages.add(cursor.getString(3).toInt())
            }
        }
    }
}